package cn.jsprun.dao.otherset;

import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.jsprun.domain.Crons;
import cn.jsprun.utils.HibernateUtil;

public class CronsSetDaoImp implements CronsSetDao {
	
	public boolean addCrons(Crons crons) {
		Transaction ts = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			ts = session.beginTransaction();
			session.save(crons);
			ts.commit();
			return true;
		} catch (HibernateException e) {
			if(ts!=null){
				ts.rollback();
			}
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean delAnyCrons(String[] deleteIds) {
		Transaction ts = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			ts = session.beginTransaction();
			int len = deleteIds.length;
			Crons crons = null;
			for(int i=0;i<len;i++){
				crons = (Crons)session.get(Crons.class, Integer.parseInt(deleteIds[i]));
				session.delete(crons);
			}
			ts.commit();
			return true;
		} catch (HibernateException e) {
			if(ts!=null){
				ts.rollback();
			}
			e.printStackTrace();
		}
		return false;
	}
	public Crons queryCronsById(Integer id) {
		Transaction ts = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			ts = session.beginTransaction();
			Crons crons = (Crons) session.get(Crons.class, id);
			ts.commit();
			return crons;
		} catch (HibernateException e) {
			if(ts!=null){
				ts.rollback();
			}
			e.printStackTrace();
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<Crons> queryAllCrons() {
		Transaction ts = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			ts = session.beginTransaction();
			List<Crons> cronsList = session.createQuery("from Crons order by cronid,lastrun desc,nextrun desc").list();
			ts.commit();
			return cronsList;
		} catch (HibernateException e) {
			if(ts!=null){
				ts.rollback();
			}
			e.printStackTrace();
		}
		return null;
	}
	@SuppressWarnings("unchecked")
	public List<Crons> queryCronsByCondition(String sqlStatement) {
		Transaction ts = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			ts = session.beginTransaction();
			List<Crons> cronsList = session.createSQLQuery(sqlStatement).addEntity(Crons.class).list();
			ts.commit();
			return cronsList;
		} catch (HibernateException e) {
			if(ts!=null){
				ts.rollback();
			}
			e.printStackTrace();
		}
		
		return null;
	}
	public boolean updateCrons(Crons crons) {
		Transaction ts = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			ts = session.beginTransaction();
			session.update(crons);
			ts.commit();
			return true;
		} catch (HibernateException e) {
			if(ts!=null){
				ts.rollback();
			}
			e.printStackTrace();
		}
		return false;
	}
	public boolean updateCrons(List<Crons> cronsList) {
		Transaction ts = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			ts = session.beginTransaction();
			Iterator<Crons> cronsIt = cronsList.iterator();
			Crons crons = null;
			while(cronsIt.hasNext()){
				crons = cronsIt.next();
				session.update(crons);
			}
			ts.commit();
			return true;
		} catch (HibernateException e) {
			if(ts!=null){
				ts.rollback();
			}
			e.printStackTrace();
		}
		return false;
	}

}
